package cc.comac.mom.controller;

import java.io.File;
import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairService;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairBean;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.JasperHelper;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value ="/EquipRepair")
public class EquipRepairController {

	@Autowired
	private EquipRepairService equipRepairService;
	
	@Autowired
	private BaseConnectionData baseConnectionData;
	
	@RequestMapping(value = "/selectByDevNumberAndFaultCause",method = RequestMethod.POST)
	public ResultBean selectByDevNumberAndFaultCause(@RequestBody EquipRepairBean obj) {
		
		return equipRepairService.selectByDevNumberAndFaultCause(obj);		
	}
	
	@RequestMapping(value = "/updateFaultCauseById",method = RequestMethod.POST)
	public ResultBean updateFaultCauseById(@RequestBody EquipRepair obj) {
		return equipRepairService.updateFaultCauseById(obj);

	}
	
	@ApiOperation(value = "故障提报")
	@RequestMapping(value = "/insetFaultInfo",method = RequestMethod.POST)
	public ResultBean insetFaultInfo(@RequestBody EquipRepair obj) {
		return equipRepairService.insetFaultInfo(obj);
	}
	
	@ApiOperation(value = "通过id获取维修信息")
	@RequestMapping(value = "/getById/{id}",method = RequestMethod.GET)
	public ResultBean getById(@PathVariable long id) {
		// TODO Auto-generated method stub
		return equipRepairService.getById(id);
	}

	@ApiOperation(value = "分配")
	@RequestMapping(value = "/allot",method = RequestMethod.POST)
	public ResultBean allot(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairService.allot(obj);
	}

	@ApiOperation(value = "验收")
	@RequestMapping(value = "/acceptance",method = RequestMethod.POST)
	public ResultBean acceptance(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairService.acceptance(obj);
	}

	@ApiOperation(value = "维修分析")
	@RequestMapping(value = "/repairAnalyze",method = RequestMethod.POST)
	public ResultBean repairAnalyze(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairService.repairAnalyze(obj);
	}

	@ApiOperation(value = "维修开始")
	@RequestMapping(value = "/repairBegin",method = RequestMethod.POST)
	public ResultBean repairBegin(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairService.repairBegin(obj);
	}

	@ApiOperation(value = "维修结束")
	@RequestMapping(value = "/repairEnd",method = RequestMethod.POST)
	public ResultBean repairEnd(@RequestBody EquipRepair obj) {
		// TODO Auto-generated method stub
		return equipRepairService.repairEnd(obj);
	}
	
	@ApiOperation(value = "派工")
	@RequestMapping(value = "/dispatch",method = RequestMethod.POST)
	public ResultBean dispatch(@RequestBody EquipRepair obj) {
		return equipRepairService.dispatch(obj);
	}
	@ApiOperation(value = "分页查询维修信息")
	@RequestMapping(value = "/selectByCondition",method = RequestMethod.POST)
	public ResultBean selectByCondition(@RequestBody EquipRepair obj) {
		return equipRepairService.selectByCondition(obj);
	}
	
	@ApiOperation(value = "维修导出数据")
	@RequestMapping(value = "/toExportExcel",method = RequestMethod.POST)  
	public void exportExcel(HttpServletRequest request, HttpServletResponse response,@RequestBody EquipRepair obj) {
		String filePath = equipRepairService.exportData(request, response, obj);
		ExportExcel.download(filePath, response);
		ExportExcel.createDir(filePath);
	}
	
	@ApiOperation(value = "委外确认")
	@RequestMapping(value = "/outsourcingConfirm",method = RequestMethod.POST)  
	public ResultBean outsourcingConfirm(@RequestBody EquipRepair obj) {
		ResultBean outsourcingConfirm = equipRepairService.outsourcingConfirm(obj);
		return outsourcingConfirm;
	}
	@ApiOperation(value = "通过维修主键生成维修pdf数据")
	@RequestMapping(value = "/printRepairInfoPdf/{repairId}",method = RequestMethod.GET) 
	public ResultBean printRepairInfoPdf(@PathVariable("repairId") long repairId,HttpServletRequest request,HttpServletResponse response) {
		ResultBean resultBean = new ResultBean(true);
		try {
			Map<String, String> printRepairInfoPdf = equipRepairService.printRepairInfoPdf(repairId);
			//指定导出文件名称
	        String exportFilePath="repair";
	        //指定模板文件
	        ClassPathResource classPathResource = new ClassPathResource("config/repair.jasper");
	        File file=classPathResource.getFile();
	        Connection connection = baseConnectionData.getConnection();
	        JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, printRepairInfoPdf, connection);
	        baseConnectionData.closeConnection(connection);
		} catch (Exception e) {
			e.printStackTrace();
			resultBean.setErrorMessge(e.getMessage());
		}
		return resultBean;
	}

	@ApiOperation(value = "执行汇总")
	@RequestMapping(value = "/queryExecuteCount",method = RequestMethod.GET)
	public ResultBean queryExecuteCount() {
		ResultBean result = equipRepairService.queryExecuteCount();

		return result;
	}
}
